<%@ page import="com.web.test.bean.Student" %>
<%@ page import="com.web.test.dao.StudentDao" %>
<%@ page import="com.web.test.utils.SQLUtils" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/9/20
  Time: 19:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>考生列表</title>
</head>

<style>

    body {
        background-color: rgb(241, 240, 245);
    }

    td {
        width: 100px;
    }

    table {
        margin-top: 60px;
        background-color: white;
    }
</style>

<%
    List<Student> students = null;
    int totalCount = 0;
    //多少页
    int pageNumber = 0;

    //每页多少条数据
    int pageCount = 4;
    //当前页
    int currentPage = 1;


    if (request.getParameter("whichPage") != null) {
        currentPage = Integer.parseInt(request.getParameter("whichPage"));
    }

    StudentDao studentDao = new StudentDao();

    if (request.getParameter("deleteId") != null) {

        boolean falg = studentDao.updateAndDelete(SQLUtils.getDeleteByStuNoSql(Integer.parseInt(request.getParameter("deleteId"))));

        currentPage = (Integer) session.getAttribute("currentPage");

    }

    List<Student> totalStudents = studentDao.find(SQLUtils.stuAll);
    totalCount = totalStudents.size();

    //每页数据
    students = studentDao.page(pageCount, currentPage);
    pageNumber = (int) Math.ceil((float) totalCount / Float.valueOf(pageCount));


    session.setAttribute("currentPage", currentPage);


%>
<body>


<div class="right">
    <center>
        <table border="1px" cellspacing="0px" cellpadding="20px">
            <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>专业</th>
                <th>学校</th>
                <th>操作</th>
            </tr>
            </thead>
            <%
                if (students != null) {

                    for (Student student : students) { %>

            <tr>
                <td><%=student.getStuNo()%>
                </td>
                <td><%=student.getName() %>
                </td>
                <td><%=student.getAge() %>
                </td>
                <td><%=student.getMajor() %>
                </td>
                <td><%=student.getCollege() %>
                </td>
                <td><a href="webexam_list.jsp?deleteId=<%=student.getStuNo()%>">删除</a>

                    <%--<a href="webexam_list.jsp?deleteId=<%=student.getStuNo()%>">删除</a>--%>
                </td>
            </tr>

            <%
                }
            } else {%>

            <tr>
                <td>暂无数据</td>
            </tr>
            <%
                }

            %>

            <tr>
                <td colspan="7" align="right">总共:<%=pageNumber%>页,<%=totalCount%>条记录,当前页：<%=currentPage%>
                    跳转<input type="text" style="width: 30px" value="<%=currentPage%>"
                             onkeydown="jump_page(this)">

                    <a href="#" onclick="last_page()">上一页</a>
                    <a href="#" onclick="next_page()">下一页</a>

                </td>

            </tr>

        </table>
    </center>
</div>
<script type="text/javascript">

    function jump_page(i) {

        if (event.keyCode == 13) {

            if (i.value <= 0) {
                alert("不能输入负值和0");
            } else if (i.value <= <%=pageNumber%>) {

                window.location = "webexam_list.jsp?whichPage=" + i.value;

            } else {

                alert("不能大于总页数");
            }

        }
    }

    function last_page() {

        if (1 == <%=currentPage%>) {
            alert("当前已是第一页");
        } else {
            window.location = "webexam_list.jsp?whichPage=" + (<%=currentPage%> -1);
        }

    }

    function next_page() {

        if (<%=currentPage%> == <%=pageNumber%>) {
            alert("当前已是最后一页");
        } else {
            window.location = "webexam_list.jsp?whichPage=" + (<%=currentPage%> +1);
        }

    }

</script>
</body>
</html>
